<?php

require_once 'AbstractHelper.php';

class TriggeredHelper extends AbstractHelper{

    private $callable;
    private $triggerParam;
    private $triggerValue;
    
    public function __construct(Controller $owner, $triggerParam = null, $triggerValue = null, $callable = null){
        $this->triggerParam = $triggerParam;
        $this->triggerValue = $triggerValue;
        $this->callable = $callable;
        $owner->addHelper($this);
        $owner->reserveParameter($triggerParam);
    }

    public function process(){
        $value = $this->owner->getRequest()->getParameter($this->triggerParam);
        if($this->triggerParam === null || ($value !== null && ($this->triggerValue === null || $value == $this->triggerValue))){
            if($this->callable === null)$this->onTrigger();
            else call_user_func($this->callable);
        }
    }

    public function createTriggerAddress(){
        $value = $this->triggerValue === null ? true : $this->triggerValue;
        return $this->owner->createReloadAddress(array($this->triggerParam => $value));
    }

    protected function onTrigger(){}

}
?>
